layui.define(['table', 'form', 'admin', 'upload'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    //
    table.render({
        elem: '#LAY-userrec-manage'
        , url: '/admin/dialog_config/list'
        , cols: [[
            { field: 'id', width: 70, title: 'ID', sort: true, fixed: 'left' }
            , { field: 'title', title: '弹窗名称', width: 150 }
            , {
                field: '', title: '观看时长(分)', width: 150, templet: function (d) {
                    return d.timeout
                }
            }
            , { field: 'show_times', title: '曝光量', width: 150 }
            , { field: 'login_count', title: '登录量', width: 150 }
            , { field: 'reg_count', title: '注册量', width: 150 }
            , {
                field: '', title: '转化率', width: 200, templet: function (d) {
                    return Math.floor(d.reg_count / d.show_times * 100) + "%"
                }
            }
            , {
                field: '', title: '状态', width: 100, templet: function (d) {
                    if (d.status == 1) {
                        return '<span class="layui-badge layui-bg-blue">生效</span>';
                    }
                    if (d.status == 2) {
                        return '<span class="layui-badge layui-bg">关闭</span>';
                    }
                    return '<span class="layui-badge layui-bg-gray">异常</span>';
                }
            }
            , { field: 'created_at', title: '操作时间', width: 200 }
            , { title: '操作', width: 200, align: 'center', fixed: 'right', toolbar: '#table-userreclist-operation' }
        ]]
        , page: true
        , limit: 20
        , height: 'full-180'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    //监听工具条
    table.on('tool(LAY-userrec-manage)', function (obj) {
        if (obj.event === 'del') {
            layer.confirm('确定删除？', function (index) {
                admin.req({
                    url: '/admin/dialog_config/delete?id=' + obj.data.id
                    , data: { "id": obj.data.id }
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-userrec-manage");
                            layer.msg(res.msg, { icon: 1 });
                        } else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    }
                });
            });
        }
        if (obj.event === 'enable') {
            layer.confirm('确定开启？', function (index) {
                admin.req({
                    url: '/admin/dialog_config/status'
                    , data: { "id": obj.data.id, "status": "1" }
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-userrec-manage");
                            layer.msg(res.msg, { icon: 1 });
                        } else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    }
                });
            });
        }
        if (obj.event === 'disable') {
            layer.confirm('确定关闭？', function (index) {
                admin.req({
                    url: '/admin/dialog_config/status'
                    , data: { "id": obj.data.id, "status": "2" }
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-userrec-manage");
                            layer.msg(res.msg, { icon: 1 });
                        } else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    }
                });
            });
        }
        if (obj.event === 'edit') {
            layer.open({
                type: 2,
                title: "编辑",
                content: "/admin/dialog_config/edit?id=" + obj.data.id,
                maxmin: true,
                area: ["1000px", "600px"],
            });
        }
    });

    //上传图片
    var iconSrc = $("#LAY_iconImgSrc");
    upload.render({
        url: "/admin/index/noOperation",
        elem: "#LAY_iconImgUpload",
        auto: true,
        choose: function (obj) {
            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: "/admin/live/signForCos",
                success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime,
                                });
                            },
                            Domain:res.data.imgDomain,
                        });
                        cos.putObject(
                            {
                                Bucket: res.data.bucket,
                                Region: res.data.region,
                                Key: "images/" + res.data.filename + choosefile.name,
                                Body: choosefile,
                            },
                            function (err, data) {
                                layer.closeAll("loading");
                                if (err) {
                                    console.log("上传失败", err);
                                    layer.msg("上传失败", { icon: 5 });
                                } else {
                                    if (data.statusCode == 200) {
                                        iconSrc.val("https://" + data.Location);
                                    } else {
                                        layer.msg("图片上传失败", { icon: 5 });
                                    }
                                }
                            }
                        );
                    } else {
                        layer.closeAll("loading");
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
            });
        },
    });
    //上传图片
    var iconSrc1 = $("#LAY_iconImgSrc1");
    upload.render({
        url: "/admin/index/noOperation",
        elem: "#LAY_iconImgUpload1",
        auto: true,
        choose: function (obj) {
            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: "/admin/live/signForCos",
                success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime,
                                });
                            },
                            Domain:res.data.imgDomain,
                        });
                        cos.putObject(
                            {
                                Bucket: res.data.bucket,
                                Region: res.data.region,
                                Key: "images/" + res.data.filename + choosefile.name,
                                Body: choosefile,
                            },
                            function (err, data) {
                                layer.closeAll("loading");
                                if (err) {
                                    console.log("上传失败", err);
                                    layer.msg("上传失败", { icon: 5 });
                                } else {
                                    if (data.statusCode == 200) {
                                        iconSrc1.val("https://" + data.Location);
                                    } else {
                                        layer.msg("图片上传失败", { icon: 5 });
                                    }
                                }
                            }
                        );
                    } else {
                        layer.closeAll("loading");
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
            });
        },
    });
    //上传图片
    var iconSrc2 = $("#LAY_iconImgSrc2");
    upload.render({
        url: "/admin/index/noOperation",
        elem: "#LAY_iconImgUpload2",
        auto: true,
        choose: function (obj) {
            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: "/admin/live/signForCos",
                success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime,
                                });
                            },
                            Domain:res.data.imgDomain,
                        });
                        cos.putObject(
                            {
                                Bucket: res.data.bucket,
                                Region: res.data.region,
                                Key: "images/" + res.data.filename + choosefile.name,
                                Body: choosefile,
                            },
                            function (err, data) {
                                layer.closeAll("loading");
                                if (err) {
                                    console.log("上传失败", err);
                                    layer.msg("上传失败", { icon: 5 });
                                } else {
                                    if (data.statusCode == 200) {
                                        iconSrc2.val("https://" + data.Location);
                                    } else {
                                        layer.msg("图片上传失败", { icon: 5 });
                                    }
                                }
                            }
                        );
                    } else {
                        layer.closeAll("loading");
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
            });
        },
    });

    form.on('submit(userrec-add)', function (obj) {
        //提交
        admin.req({
            url: '/admin/userrec/add_post'
            , data: obj.field
            , success: function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg, { icon: 1 });
                    setTimeout(function () {
                        window.location.reload();
                    }, 1200);
                } else {
                    layer.msg(res.msg, { icon: 5 });
                }
            }
        });
        return false;
    });

    exports('dialog_config', {})
});